# Program variables
block_speed = -6 # controls how fast the pipes move across the stage
block_gap = 200 # controls the space between the top and bottom of each pipe
block_interval = 1 # controls how often a new pipe appears
gravity = 8 # controls how fast the sprite falls
flappiness = 5 # controls how much the sprite moves up
# Store and display score
score = 0
score_text = codesters.Text("Score: " + str(score), 100, 200, "yellow")
# Setup the stage and sprite
stage.set_background("scrollingspace")
stage.disable_all_walls()
rex = codesters.Sprite("dinosaur")
rex.set_size(0.4)
rex.set_say_color("yellow")
rex.say("TAP THE SPACE BAR TO GUIDE ME THROUGH THE BLOCKS! ROAR!", 3)
rex.go_to(-200, 0)
stage.set_gravity(gravity)
# Movement controls
def space_bar():
rex.jump(flappiness)
stage.event_key("space", space_bar)
# Interval event constantly creates obstacles and increases score
def interval():
global score
make_blocks() # make the obstacles
score += 1 # add one to the score
score_text.set_text("Score: " + str(score))
stage.event_interval(interval, block_interval)
# Function to make each pipe obstacle randomly
def make_blocks():
y1 = random.randint(-250, -100) # y position of lower block
block1 = codesters.Rectangle(250, y1, 100, 300, "red")
block1.set_gravity_off()
block1.set_x_speed(block_speed)
y2 = y1 + 150 + block_gap + 150 # y position of upper block (y of lower block + 1/2 lower length + block_gap + 1/2 upper length)
block2 = codesters.Rectangle(250, y2, 100, 300, "red")
block2.set_gravity_off()
block2.set_x_speed(block_speed)
# Collision event ends the game
def collision(sprite, hit_sprite):
rex.set_physics_off() # stop the sprite from moving
stage.event_interval(None) # stop the interval event from making more obstacles and increasing score
message = "GAME OVER. REX COLLECTED " + str(score) + " POINTS!"
game_over_text = codesters.Text(message, 0, 0, "yellow")
rex.event_collision(collision)
tester = TestManager()
tester.display_success_message("Great job!")
-
Run Code
-
Activity Submitted!
Submit Work
-
Next Activity
-
Stop Running Code
-
Show Chart
-
Show Console
-
Reset Code Editor
-
Codesters How To (opens in a new tab)